// -*- C++ -*-

//=============================================================================
/**
*  @file    Transcode.h
*
* This file declares functions to convert char string among different
* unicode encoding (utf8, utf16, utf32)
*
*  Transcode.h,v 1.11 2005/01/05 14:06:12 jwillemsen Exp
*
*  @this code if from ace
*  @modify William chen <cppfans@163.com>
*/
//=============================================================================

#ifndef COMMON_UNIT_UTFTRANSCODE_H
#define COMMON_UNIT_UTFTRANSCODE_H



/**
* @class MYXML_Transcoder Transcode.h "ACEXML/common/Transcode.h"
*
* @brief MYXML_Transcoder
*
* Wrapper class for performing transcoding among different UNICODE
* this source if form ace
* modify by william.chen cppfans@163.com
* encoding.
*/
class CTranscoder
{
public:
/*
* Status of the conversion function.
	*/
	enum
	{
		TRANS_SUCCESS = 0,
		TRANS_DESTINATION_TOO_SHORT = -1,
		TRANS_END_OF_SOURCE = -2,
		TRANS_INVALID_ARGS = -3,
		TRANS_IS_SURROGATE = -4,
		TRANS_NON_UNICODE = -5
	} TRANS_STATUS;
		
		
	// The following functions translate a unicode characters
	// into different encoding.  Return number of characters put into
	// destination or consumed from src if success without
	// error, otherwise, return corresponding error code.
	/*
	* Convert a UTF-16 character into a string in UTF-8 encoding.
	*
	* @return number of characters the function uses to store the
	*         converted string if it succeeds or one of the error STATUS
	*         otherwise.
	*/
	static int utf162utf8 (wchar_t src,
		char *dst,
		size_t len);
		
		/*
		* Convert a UCS-4 character into a string in UTF-8 encoding.
		*
		* @return number of characters the function uses to store the
		*         converted string if it succeeds or one of the error STATUS
		*         otherwise.
	*/
	static int ucs42utf8 (long src,
		char *dst,
		size_t len);
		
		/*
		* Convert a UCS-4 character into a string in UTF-16 encoding.
		*
		* @return number of characters the function uses to store the
		*         converted string if it succeeds or one of the error STATUS
		*         otherwise.
	*/
	static int ucs42utf16 (long src,
		wchar_t *dst,
		size_t len);
		
		/*
		* Convert a UTF-16 surrogate character pair into a string in UTF-8 encoding.
		*
		* @return number of characters the function uses to store the
		*         converted string if it succeeds or one of the error STATUS
		*         otherwise.
	*/
	static int surrogate2utf8 (wchar_t high,
		wchar_t low,
		char *dst,
		size_t len);
		
		/*
		* Convert a UTF-16 surrogate character pair into a UCS-4 character.
		*
		* @return SUCCESS if it succeeds or one of the error STATUS
		*         otherwise.
	*/
	static int surrogate2ucs4 (wchar_t high,
		wchar_t low,
		long &dst);
		
		/*
		* Convert the first UNICODE character in a UTF-8 character string
		* into a UCS-4 character.
		*
		* @return number of characters the function consumed from the
		*         UTF-8 string if it succeeds or one of the error STATUS
		*         otherwise.
	*/
	static int utf82ucs4 (const char *src,
		size_t len,
		long &dst);
		
		/*
		* Convert the first UNICODE character in a UTF-16 character string
		* into a UCS-4 character.
		*
		* @return number of characters the function consumed from the
		*         UTF-16 string if it succeeds or one of the error STATUS
		*         otherwise.
	*/
	static int utf162ucs4 (const wchar_t *src,
		size_t len,
		long &dst);
		
	//    static int utf82utf16 (const char *src,
	//                           size_t len,
	//                           wchar_t &dst);
	// This function does not handle surrogates.
		
	// = The following functions are non-inlined:
		
	/*
	* Convert a UTF-8 string into a UTF-16 string.
	*
	* @param len The length of @a dst string.
	*
	* @return number of characters the function consumed from the
	*         UTF-8 string if it succeeds or one of the error STATUS
	*         otherwise.
	*/
	static int utf8s2utf16s (const char *src,
		wchar_t *dst,
		size_t len);
		
	/*
	* Convert a UTF-16 string into a UTF-8 string.
	*
	* @param len The length of @a dst string.
	*
	* @return number of characters the function uses in
	*         UTF-8 string if it succeeds or one of the error STATUS
	*         otherwise.
	*/
	static int utf16s2utf8s (const wchar_t *src,
		char *dst,
		size_t len);
};

#endif /* _MYXML_TRANSCODE_H_ */
